/**
 * Created with JetBrains WebStorm.
 * User: Administrator
 * Date: 14-6-16
 * Time: 下午1:53
 * To change this template use File | Settings | File Templates.
 */
define(function(require,exports,module){
    var SERSION = "1.0",
        jQuery = $ = require("jquery-1.8.2.min");

    var defaults={
        scrollName:'scrollUp',
        topDistance:100,
        topSpeed:300,
        animation:'fade',
        animationInspeed:200,
        animationOutspeed:200,
        scrollText:'回到顶部',
        scrollImg:false,
        activeOverlay:false
    };

    function scrollTop(options){
        this.options = $.extend({},defaults,options);
        this.scrollId = '#' + this.options.scrollName;
        this.init()
    }

    scrollTop.prototype = {
        init:function(){
            var options = this.options,
                scrollId = this.scrollId;
            $('<a/>',{
                id: options.scrollName,
                href:'#top',
                title:options.scrollName
            }).appendTo('body');
            $(scrollId).text(options.scrollText).css({'display':'none','position':'fixed','z-index':'999'});
            this.bindEvent();
        },
        bindEvent:function(){
            var options = this.options,
                scrollId = this.scrollId;
            $(window).scroll(function(){
                switch (options.animation){
                    case 'fade':
                        $($(window).scrollTop() > options.topDistance ? $(scrollId).fadeIn(options.animationInspeed) : $(scrollId).fadeOut(options.animationOutspeed));
                        break;
                    case 'slide':
                        $($(window).scrollTop() > options.topDistance ? $(scrollId).slideDown(options.animationInSpeed) : $(scrollId).slideUp(options.animationOutSpeed));
                        break;
                    default:
                        $( ($(window).scrollTop() > options.topDistance) ? $(scrollId).show(0) : $(scrollId).hide(0) );
                }
            });
            $(scrollId).bind('click',function(event) {
                $('html, body').animate({scrollTop:0}, options.topSpeed);
                event.preventDefault();
            });
        }

    };

    return scrollTop;
});